package lrl.nsfw.role.action;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionContext;

import lrl.core.action.BaseAction;
import lrl.core.constant.Constant;
import lrl.nsfw.role.entity.Role;
import lrl.nsfw.role.entity.RolePrivilege;
import lrl.nsfw.role.entity.RolePrivilegeId;
import lrl.nsfw.role.service.RoleService;

public class RoleAction extends BaseAction {

	@Resource
	private RoleService roleService;
	private List<Role> roleList;
	private Role role;
	private String[] privilegeIds;
	
	public String listUI() {
		ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);
		roleList = roleService.findObjects();
		return "listUI";
	}
	
	public String addUI() {
		ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);
		return "addUI";
	}
	
	public String add() {
		if (role != null) {
			if (privilegeIds != null) {
				HashSet<RolePrivilege> set = new HashSet<RolePrivilege>();
				for (int i = 0; i < privilegeIds.length; i++) {
					set.add(new RolePrivilege(new RolePrivilegeId(role, privilegeIds[i])));
				}
				role.setRolePrivileges(set);
			}
			roleService.save(role);
		}
		
		return "list";
	}
	
	public String editUI() {
		ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);
		return "editUI";
	}
	
	public String edit() {
		
		return "list";
	}
	
	public String delete() {
		
		return "list";
	}
	
	public String deleteSelected() {
		
		return "list";
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String[] getPrivilegeIds() {
		return privilegeIds;
	}

	public void setPrivilegeIds(String[] privilegeIds) {
		this.privilegeIds = privilegeIds;
	}
}
